<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
    <th:block th:include="include :: header('订单派单记录列表')"/>
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <div class="col-sm-12 search-collapse">
            <form id="formId" class="layui-form">
                <div class="layui-form-item">
                    <label class="layui-form-label">订单编号：</label>
                    <div class="layui-input-inline">
                        <input type="text" name="orderNumber" placeholder="请输入订单编号"
                               class="layui-input" lay-required/>
                    </div>


                    <label class="layui-form-label">客户昵称:</label>
                    <div class="layui-input-inline">
                        <input type="text" name="clientName" placeholder="请输入客户昵称"
                               class="layui-input"/>
                    </div>

                    <label class="layui-form-label">订单来源:</label>
                    <div class="layui-input-inline">
                        <select name="orderSource" dict-code="order_source">
                            <option value="">所有</option>
                        </select>
                    </div>

                    <label class="layui-form-label">订单状态:</label>
                    <div class="layui-input-inline">
                        <select name="orderStatus" dict-code="order_status">
                            <option value="">所有</option>
                        </select>
                    </div>

                    <div class="layui-inline mt5" style="margin-top: 20px">

                        <label class="layui-form-label">三方订单：</label>
                        <div class="layui-input-inline">
                            <input type="text" name="thirdOrderNumber" placeholder="请输入三方订单编号"
                                   class="layui-input" lay-required/>
                        </div>


                        <label class="layui-form-label">创建时间:</label>
                        <div class="layui-input-inline input-sm">
                            <input class="layui-input" id="startDate" name="startDate" placeholder="开始时间"
                                   type="text"/>
                        </div>
                        <span class="layui-form-mid">-</span>
                        <div class="layui-input-inline input-sm">
                            <input class="layui-input" id="endDate" name="endDate" placeholder="结束时间"
                                   type="text"/>
                        </div>

                        <button class="pear-btn pear-btn-md pear-btn-primary mt5" lay-submit
                                lay-filter="orderDispatch-query">
                            <i class="layui-icon layui-icon-search"></i>
                            查询
                        </button>
                        <button type="reset" class="pear-btn pear-btn-md mt5">
                            <i class="layui-icon layui-icon-refresh"></i>
                            重置
                        </button>

                    </div>


                </div>
            </form>
        </div>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="orderDispatch-table" lay-filter="orderDispatch-table"></table>
        <script type="text/html" id="orderDispatch-toolbar">
            <button sec:authorize="hasPermission('/sys/orderDispatch/add','sys:orderDispatch:add')"
                    class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
                <i class="layui-icon layui-icon-add-1"></i>
                新增
            </button>
            <button sec:authorize="hasPermission('/sys/orderDispatch/remove','sys:orderDispatch:remove')"
                    class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
                <i class="layui-icon layui-icon-delete"></i>
                删除
            </button>
        </script>
        <script type="text/html" id="orderDispatch-bar">
            <button sec:authorize="hasPermission('/sys/orderDispatch/edit','sys:orderDispatch:edit')"
                    class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i
                    class="layui-icon layui-icon-edit"></i>
            </button>
            <button sec:authorize="hasPermission('/sys/orderDispatch/remove','sys:orderDispatch:remove')"
                    class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i
                    class="layui-icon layui-icon-delete"></i>
            </button>
        </script>
    </div>
</div>

<script id="order-source" type="text/html">
    {{#if (d.orderSource == 1) { }}
    <span>小红书</span>
    {{# }else if(d.orderSource == 2){ }}
    <span>闲鱼</span>
    {{# }else if(d.orderSource == 3){ }}
    <span>淘宝</span>
    {{# }else if(d.orderSource == 4){ }}
    <span>抖音</span>
    {{# } }}
</script>

<script id="order-status" type="text/html">
    {{#if (d.orderStatus == 1) { }}
    <span style="color: grey">已下单</span>
    {{# }else if(d.orderStatus == 2){ }}
    <span>已完成</span>
    {{# }else if(d.orderStatus == 3){ }}
    <span style="color: grey">待结算</span>
    {{# }else if(d.orderStatus == 4){ }}
    <span style="color: #00ff80">已结算</span>
    {{# } }}
</script>

<script id="self-make" type="text/html">
    {{#if (d.isSelfMake == 1) { }}
    <span style="color: #00ff80">是</span>
    {{# }else if(d.isSelfMake == 0){ }}
    <span>否</span>
    {{# } }}
</script>


<th:block th:include="include :: footer"/>
<script>
    layui.use(['table', 'form', 'jquery', 'dictionary', 'popup', 'laydate'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let popup = layui.popup;
        let laydate = layui.laydate;

        let prefix = "/sys/orderDispatch/";

        let insStart = laydate.render({
            elem: '#startDate',
            min: 0,
            done: function (value, date) {
                insEnd.config.min = lay.extend({}, date, {
                    month: date.month - 1
                });
                insEnd.config.elem[0].focus();
            }
        });

        let insEnd = laydate.render({
            elem: '#endDate',
            min: 0,
            done: function (value, date) {
                insStart.config.max = lay.extend({}, date, {
                    month: date.month - 1
                });
            }
        });


        let cols = [
            [
                {type: 'checkbox'},
                {
                    field: 'orderNumber',
                    title: '订单编号',
                    align: "center",
                    width: 200
                },
                {
                    field: 'username',
                    title: '提单人员',
                    align: "center",
                    width: 200
                },
                {
                    field: 'thirdOrderNumber',
                    title: '三方订单编号',
                    align: "center",
                    width: 200
                },
                {
                    field: 'clientName',
                    title: '客户昵称',
                    align: "center",
                    width: 100
                },
                {
                    field: 'clientContact',
                    title: '联系方式',
                    align: "center",
                    width: 150
                },
                {
                    field: 'orderSource',
                    title: '订单来源',
                    align: "center",
                    templet: '#order-source',
                    width: 100
                },
                {
                    field: 'orderStatus',
                    title: '订单状态',
                    align: "center",
                    templet: '#order-status',
                    width: 100
                },
                {
                    field: 'isSelfMake',
                    title: '是否自做',
                    align: "center",
                    templet: '#self-make',
                    width: 100
                },
                {
                    field: 'groupName',
                    title: '群聊名称',
                    align: "center",
                    width: 100
                },
                {
                    field: 'assignedTo',
                    title: '接单人员',
                    align: "center",
                    width: 100
                },
                {
                    field: 'orderDetails',
                    title: '订单内容',
                    align: "center",
                    width: 100
                },
                {
                    field: 'orderAmount',
                    title: '订单金额',
                    align: "center",
                    width: 100
                },
                {
                    field: 'orderImage',
                    title: '下单图片',
                    align: "center",
                    templet: function (d) {
                        return '<img src="' + d.orderImage + '" alt="Order Image" style="width: 100px; height: auto; cursor: pointer;" onclick="viewImage(\'' + d.orderImage + '\')"/>';
                    },
                    width: 100
                },
                {
                    field: 'createTime',
                    title: '创建时间',
                    align: "center",
                    width: 150
                },
                {
                    field: 'finishTime',
                    title: '交付日期',
                    align: "center",
                    width: 150
                },

                {title: '操作', toolbar: '#orderDispatch-bar', align: 'center', width: 130}
            ]
        ]

        table.render({
            elem: '#orderDispatch-table',
            url: prefix + 'data',
            page: true,
            cols: cols,
            skin: 'line',
            toolbar: '#orderDispatch-toolbar',
            defaultToolbar: [{
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports']
        });

        table.on('tool(orderDispatch-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            }
        });

        table.on('toolbar(orderDispatch-table)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            }
        });

        form.on('submit(orderDispatch-query)', function (data) {
            let queryParams = {
                ...data.field,  // 表单数据
                startDate: $('#startDate').val(),  // 获取开始时间
                endDate: $('#endDate').val()       // 获取结束时间
            };
            table.reload('orderDispatch-table', {where: queryParams})
            return false;
        });

        window.add = function () {
            layer.open({
                type: 2,
                title: '新增订单',
                shade: 0.1,
                area: ['550px', '500px'],
                content: prefix + 'add'
            });
        }

        window.edit = function (obj) {
            layer.open({
                type: 2,
                title: '修改订单',
                shade: 0.1,
                area: ['550px', '500px'],
                content: prefix + 'edit?id=' + obj.data['id']
            });
        }

        window.remove = function (obj) {
            layer.confirm('确定要删除该订单派单记录', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: prefix + "remove/" + obj.data['id'],
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.success) {
                            layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                                obj.del();
                            });
                        } else {
                            layer.msg(result.msg, {icon: 2, time: 1000});
                        }
                    }
                })
            });
        }

        window.batchRemove = function (obj) {
            let data = table.checkStatus(obj.config.id).data;
            if (data.length === 0) {
                layer.msg("未选中数据", {icon: 3, time: 1000});
                return false;
            }
            let ids = "";
            for (let i = 0; i < data.length; i++) {
                ids += data[i].id + ",";
            }
            ids = ids.substr(0, ids.length - 1);
            layer.confirm('确定要删除这些订单派单记录', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: prefix + "batchRemove",
                    dataType: 'json',
                    data: {"ids": ids},
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.success) {
                            layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                                table.reload('orderDispatch-table');
                            });
                        } else {
                            layer.msg(result.msg, {icon: 2, time: 1000});
                        }
                    }
                })
            });
        }

        window.refresh = function (param) {
            table.reload('orderDispatch-table', {where: param});
        }

        window.viewImage = function (imageUrl) {
            layer.open({
                type: 1,  // Custom content
                title: '订单图片',
                area: ['80%', '80%'],  // Size of the modal
                content: '<img src="' + imageUrl + '" alt="Order Image" style="width: 100%; height: auto;" />',
                shadeClose: true  // Clicking outside closes the modal
            });
        }
    })
</script>
</body>
</html>
